package com.gt.quality.job;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gt.quality.system.entity.SysOperLog;
import com.gt.quality.system.service.SysOperLogService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @description: 定时任务
 * // 每 5 秒执行一次
 * v->@Scheduled(fixedRate = 5000)
 * // 每次任务完成后等待 2 秒再执行下一次
 * v->@Scheduled(fixedDelay = 2000)
 * // 在每天的 10:00 AM 执行
 * v->@Scheduled(cron = "0 0 10 * * ?")
 * @author: makeJava
 * @createTime: 2025-01-01 17:53
 * @since JDK 21
 **/
@Component
@Slf4j
@AllArgsConstructor
public class ScheduledTasks {

    private final SysOperLogService sysOperLogService;

    /**
     * 每天十点清理过期日志数据
     */
    @Scheduled(cron = "0 0 10 * * ?" )
    public void scheduledClearLog() {
        log.info("在每天的 10:00 AM 执行.....开始" );
        LambdaQueryWrapper<SysOperLog> wrapper = new LambdaQueryWrapper<>();
        // 删除15天之前的数据
        wrapper.le(SysOperLog::getOperTime, LocalDateTime.now().minusDays(15));
        try {
            sysOperLogService.remove(wrapper);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        log.info("在每天的 10:00 AM 执行.....结束" );
    }

}
